﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Text;
using System.Web;

using FRL.DebtChased.Extensions;

namespace FRL.DebtChased.Repository.ViewModels
{

	/// <summary>
	/// Declaration of CommTemplateListModel class
	/// </summary>
	public class CommTemplateListModel
	{

		/////////////////////////////////////////////////////////////
		// Nested classes

		public class Element
		{

			/////////////////////////////////////////////////////////
			// Attributes

			internal int m_iCommTemplateId = 0;
			public int CommTemplateId
			{
				get { return m_iCommTemplateId; }
			}

			public Definitions.eCommTemplateType TemplateType { get; set; }

			public string Name { get; set; }

			internal string m_strSummaryLine;
			public string SummaryLine
			{
				get { return m_strSummaryLine; }
			}

			public string LastModified { get; set; }

			internal int m_iUsageCount;
			public int UsageCount
			{
				get { return m_iUsageCount; }
			}

			public string ImagePath
			{
				get
				{
					string strFtmp;
					switch (this.TemplateType)
					{
						case Definitions.eCommTemplateType.Email:
							strFtmp = "~/Images/HtmlIcon20x20.jpg";
							break;
						case Definitions.eCommTemplateType.Letter:
							strFtmp = "~/Images/WordIcon20x20.jpg";
							break;
						default:
#if DEBUG
							Debug.Assert(false);
#endif // #if DEBUG
							strFtmp = null;
							break;
					} // switch
					return strFtmp;
				}
			}

		}

		/////////////////////////////////////////////////////////////
		// Constants and Enumerations

		public static readonly int SummaryLength = 60;

		/////////////////////////////////////////////////////////////
		// Attributes

		private List<Element> m_elements = new List<Element>();
		public List<Element> Elements
		{
			get { return m_elements; }
		}

		/////////////////////////////////////////////////////////////
		// Construction

		public CommTemplateListModel(DebtChasedEntities ent, int iUserId)
		{

			try
			{

				// Load the comm-templates
				var results = ent.CommTemplatesProc();
				foreach (var ob in results)
				{

					// Add an element
					Element el = new Element();
					m_elements.Add(el);
					el.m_iCommTemplateId = ob.fdCommTemplateId;
					el.Name = ob.fdName;
					el.TemplateType = (Definitions.eCommTemplateType)ob.fdTemplateType;
					el.m_iUsageCount = ob.fdUsageCount;

					// Build a summary line
					if (el.TemplateType == Definitions.eCommTemplateType.Letter)
						el.m_strSummaryLine = ob.fdSummaryLine;
					else if (el.TemplateType == Definitions.eCommTemplateType.Email)
						el.m_strSummaryLine = MakeSummaryLine(ob.fdBody, true).DotDotDot(SummaryLength);
#if DEBUG
					else
						Debug.Assert(false);
#endif // #if DEBUG

					// If there is modified objects...
					if (ob.fdModifiedBy != null)
						el.LastModified = string.Format("{0} by {1}", ob.fdModifiedAt.Value.ToCuteString(), ob.fdModifiedBy);

				}

			}
			catch (Exception x)
			{

				// Handle the error
				ErrorProcessing.HandleException(x, "load template model", iUserId);

			}

		}

		/////////////////////////////////////////////////////////////
		// Static operations

		public static string MakeSummaryLine(string strIn, bool bHtml)
		{

			// Declare a string-builder
			StringBuilder sb = new StringBuilder(strIn);

			// Remove all escape characters
			int i1 = 0;
			while (i1 < sb.Length)
			{
				char c1 = sb[i1];
				if (((int)c1) < 0x20)
					sb.Remove(i1, 1);
				else
					i1++;
			}

			// Remove the marker-up
			while (true)
			{

				// Find the next "<"
				int iPos = -1;
				for (i1 = 0; i1 < sb.Length; i1++)
				{
					if (sb[i1] == '<')
					{
						iPos = i1;
						break;
					}
				} // for-loop
				if (iPos == -1)
					break;

				// Find closing '>'
				int iPos2 = -1;
				for (i1 = iPos+1; i1 < sb.Length; i1++)
				{
					if (sb[i1] == '>')
					{
						iPos2 = i1;
						break;
					}
				} // for-loop
				if (iPos2 == -1)
					break;

				// Remove the bracketed text
				sb = sb.Remove(iPos, iPos2 - iPos + 1);

			} // while-loop

			// Remove '<' and '>'
			sb = sb.Replace("<", "&lt;");
			sb = sb.Replace(">", "&gt;");

			// Return string
			return sb.ToString();

		}

	}

}
